package com.example.webdemo.config;

import com.example.webdemo.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author zp
 * @version 1.0
 * @description: Web配置类
 * @date 2023/7/27 19:04
 */
@Configuration
public class WebAppConfig implements WebMvcConfigurer {
    @Value("${upload.location}")
    private String uploadLocation;

    // 多个拦截器组成一个拦截器链
    // addPathPatterns 用于添加拦截规则
    // excludePathPatterns 用户排除拦截
    @Bean
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor())//添加拦截器
                .addPathPatterns("/api/updateImage",
                        "/api/logout",
                        "/api/deleteImage",
                        "/api/getUserInfo",
                        "/api/saveArticle",
                        "/api/addArticlePopularity",
                        "/api/deleteArticlePopularity"); //需要拦截的请求
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("static/image/**").addResourceLocations("file:"+uploadLocation);
    }
}

